package com.InfinityRun;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.util.Log;

public class Assets
{
	Context ctx;
	
	public static Bitmap player;
	public static Bitmap obstacle;
	public static MediaPlayer gameSound;
	public static Bitmap instructions;
	public static Bitmap[] star;
	public static Bitmap menu;
	public static Bitmap lose;
	
	public Assets(Context context)
	{
		star = new Bitmap[2];
		ctx = context;
		Load();
	}
	
	private void Load()
	{
		lose = LoadImage("lose.png");
		menu = LoadImage("menu.png");
		instructions = LoadImage("inst.png");
		player = LoadImage("nave.png");
		obstacle = LoadImage("obstacle.png");
		gameSound = LoadSound("gameSound.ogg");
		star[0] = LoadImage("star_1.png");
		star[1] = LoadImage("star_2.png");
	}
	
	private Bitmap LoadImage(String path)
	{		
	    AssetManager manager = ctx.getAssets();   
		String imagePath = path;
		
	    InputStream is=null;
	    
	    try 
	    {
	        is = manager.open(imagePath);
	    }
	    
	    catch (IOException e1) {  e1.printStackTrace();}

	    Bitmap aux = null;
	    
	    try 
	    {
	        aux = BitmapFactory.decodeStream(is);
	    }
	    
	    finally 
	    {
	        try 
	        {
	            is.close();
	            is = null;
	            return aux;
	        } 
	        
	        catch (IOException e)
	        {
	        	Log.d("testes", e.getMessage());
	        }
	    }
	    
	    return null;
	}
	
	private MediaPlayer LoadSound(String path)
	{
		MediaPlayer auxSound;
		auxSound = new MediaPlayer();
		try
		{
		    AssetManager assets = ctx.getAssets();	
		    AssetFileDescriptor descriptor = assets.openFd(path);
		    auxSound.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
		    auxSound.prepare();
		    auxSound.setLooping(true);
		    return auxSound;
		}
		
		catch(IOException e)
		{
			return null;
		}		
	}
}
